package com.woniuxy.main.service.imp;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.woniuxy.main.mapper.RechargeMapper;
import com.woniuxy.main.mapper.UserMapper;
import com.woniuxy.main.pojo.Recharge;
import com.woniuxy.main.pojo.User;
import com.woniuxy.main.service.RechargeService;

import lombok.Data;

@Service
@Transactional
@Data
public class RechargeServiceImp implements RechargeService {
	
	@Resource
	private RechargeMapper rechargeMapper;
	@Resource
	private UserMapper userMapper;

	@Override	//插入充值记录
	public int insertRecharge(Recharge recharge) {
		return rechargeMapper.insertRecharge(recharge);
	}

	@Override	//更新充值记录
	public String updateRecharge(Recharge recharge) { 
		int row = rechargeMapper.updateRecharge(recharge);
		if(row > 0){	//充值成功，用户余额更新
			//获取订单金额
			Recharge recharge1 = rechargeMapper.getRechargeByReid(recharge);
			//获取当前用户
			User user = userMapper.selectByUid(recharge1.getUid());
			user.setMoney(user.getMoney().add(recharge1.getReprice()));
			//更新用户余额
			userMapper.updateMoney(user);
		}
		return null;
	}

	@Override	//查询所有充值记录
	public List<Recharge> allRecharge(User user) {
		return rechargeMapper.allRecharge(user);
	}

}
